
import { LabelComponent, tween, Vec3, UITransform, view, _decorator, Component, Node, find, CameraComponent } from 'cc';
import { poolManager } from '../../../Framework/Scripts/Managers/poolManager';
import myUtil from '../../../Framework/Scripts/Utils/myUtil';
import { Vector3 } from '../../../Framework/Scripts/Utils/Vector3';
import { constant } from '../constant';
import { GameApp } from '../GameApp';

//战斗血量增减提示脚本
const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('FightTip')
export class FightTip extends Component {
    private _tweenTip: any = null;//tween实例
    private _costTime: number = 1.5;//缓动所需时间
    private _isChangePos: boolean = false;//是否改变位置
    private _oriWorPos: Vec3 = new Vec3();//初始位置
    private _oriScale: Vec3 = new Vec3(0.7, 0.7, 0.7);//初始缩放
    private _scale_1: Vec3 = new Vec3(1, 1, 1);//最开始缩放
    private _scale_2: Vec3 = new Vec3();//最终缩放
    private nowPos: Vec3 = new Vec3()
    private _offsetPos: Vec3 = new Vec3(0, 200, 0);


    start() {
    }

    /**
     * 展示血量提示
     *
     * @param {*} scriptParent 关联的血条脚本
     * @param {number} tipType 提示类型
     * @param {string} bloodNum 数值
     * @param {Function} [callback] 回调函数
     * @memberof FightTip
     */
    public show(wpos: Vec3, tipType: number, bloodNum: number, callback?: Function) {
        this._closeTweenTip();
        this.node.setScale(this._oriScale);

        this._oriWorPos.set(wpos);

        let arrChildren = this.node.children;
        arrChildren.forEach((item) => {
            item.active = false;
        })


        this.node.setSiblingIndex(constant.PRIORITY.BLOOD_TIP);
        bloodNum = Math.round(bloodNum);
        let txt = String(bloodNum);

        let ndSub: Node = null!;
        if (tipType === constant.FIGHT_TIP.ADD_BLOOD) {
            ndSub = this.node.getChildByName("addBlood") as Node;
            txt = "+" + txt;
        } else if (tipType === constant.FIGHT_TIP.REDUCE_BLOOD) {
            ndSub = this.node.getChildByName("reduceBlood") as Node;
        } else if (tipType === constant.FIGHT_TIP.CRITICAL_HIT) {
            ndSub = this.node.getChildByName("criticalHit") as Node;
            this.node.setSiblingIndex(constant.PRIORITY.BLOOD_CRITICAL_TIP);
        }

        let lbHitNum = ndSub.getChildByName('num')?.getComponent(LabelComponent);
        lbHitNum && (lbHitNum.string = txt);

        ndSub.active = true;



        this._isChangePos = true;

        this._tweenTip = tween(this.node)
            .to(this._costTime * 0.4, { scale: this._scale_1 }, { easing: 'smooth' })
            .to(this._costTime * 0.2, { scale: this._scale_2 }, { easing: "backIn" })


            .call(() => {
                ndSub.active=false
                this._closeTweenTip();
                poolManager.instance.putNode(this.node);
                callback && callback();
                this._isChangePos = false;
            })
            .start();
    }



    private _closeTweenTip() {
        if (this._tweenTip) {
            this._tweenTip.stop();
            this._tweenTip = null;
        }
    }

    update(dt: number) {

        if (this._isChangePos) {


            GameApp.Instance.Camera.getComponent(CameraComponent).convertToUINode(this._oriWorPos, find("Canvas") as Node, this.nowPos);
            this.nowPos.add(this._offsetPos);
            this.node.setPosition(this.nowPos);
        }
    }

}
